package com.teamwork.ZuccEcho.Repository;

import com.teamwork.ZuccEcho.Entity.Modelquestion;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

public interface ModelQuestionApiRepository extends JpaRepository<Modelquestion, Long>, JpaSpecificationExecutor<Modelquestion> {
    Modelquestion findById(long id);

    @Query(value="select mq_id from modelquestion where m_id = ?1", nativeQuery = true)
    List<Long> searchAllModelQuestionsByModelId(long m_id);

    @Query(value="select MAX(mq_id) from modelquestion", nativeQuery = true)
    Long findPublicedMqId();

    @Query(value="select mo_id from moptions where mq_id = ?1", nativeQuery = true)
    List<Long> searchAllModelOptionsByQuestionId(long Mq_id);

    @Query(value="select * from modelquestion where mq_id = ?1", nativeQuery = true)
    List<Modelquestion> searchByMQuestionId(long Mq_id);

    @Transactional
    @Modifying
    @Query(value="DELETE from moptions where mo_id= ?1", nativeQuery = true)
    void deleteOptionByOId(long option_Id);

    @Transactional
    @Modifying
    @Query(value="UPDATE modelquestion SET m_id = ?1 , mq_type = ?2 , mq_name = ?3 , " +
            "mq_isrequired = ?4 , mq_content = ?5 " +
            "WHERE mq_id = ?6", nativeQuery = true)
    void modifyModelQuestionByMqId(long m_id,int mq_type,String mq_name,boolean mq_isrequired,String mq_content,long mq_id);
}
